package extensionTools.smvCreator;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import extensionTools.smvCreator.Exceptions.XmlParserException;

/**
 * This class has functions required to parse the needed data out of 
 * the config.xml file
 * @author Dani Toker, Amit Yungman
 */
public class XmlParser {
	private Document _doc;
	private XPathFactory _xpathFactory;
	
	/**
	 * This constructor parse the document received
	 * @param config - the file to parse
	 * @throws XmlParserException
	 */
	public XmlParser(final InputStream config) throws XmlParserException{
			DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
			docFactory.setNamespaceAware(true);
			DocumentBuilder temp;
			try {
				temp = docFactory.newDocumentBuilder();
				_doc = temp.parse(config);
			} catch (ParserConfigurationException e) {
				throw new XmlParserException("XmlParser: problem while initializing the parser");
			} catch (SAXException e) {
				throw new XmlParserException("XmlParser: problem while initializing the parser");
			} catch (IOException e) {
				throw new XmlParserException("XmlParser: problem while initializing the parser");
			}
			_xpathFactory = XPathFactory.newInstance();
	}
	
	/**
	 * 
	 * @return K (number of instances requested)
	 * @throws XPathExpressionException
	 */
	public int getK() throws XPathExpressionException{
		return Integer.parseInt((String)_xpathFactory.newXPath().compile("//instance_number/@K").evaluate(_doc));
	}
	
	/**
	 * 
	 * @return process termination chance (the right hand border for the uniform termination)
	 * @throws XPathExpressionException
	 */
	public int getProcessTermination() throws XPathExpressionException{
		return Integer.parseInt((String)_xpathFactory.newXPath().compile("//process_termination/@ev_term").evaluate(_doc));
	}

	/**
	 * 
	 * @return system termination chance (the right hand border for the uniform termination)
	 * @throws XPathExpressionException
	 */
	public int getSystemTermination() throws XPathExpressionException{
		return Integer.parseInt((String)_xpathFactory.newXPath().compile("//system_termination/@sys_term").evaluate(_doc));
	}
	
	/**
	 * 
	 * @return environmental object creation chance (the right hand border for the uniform creation)
	 * @throws XPathExpressionException
	 */
	public int getEnvironmentalObjectCreation() throws XPathExpressionException{
		return Integer.parseInt((String)_xpathFactory.newXPath().compile("//envi_object_creation/@creation_var").evaluate(_doc));
	}
	
	/**
	 * 
	 * @return path to a folder (must be valid) to save SMV script in 
	 * @throws XPathExpressionException
	 * 
	 */
	public String getSavePath() throws XPathExpressionException{
		return _xpathFactory.newXPath().compile("//path_from_root/text()").evaluate(_doc);
	}
	
}
